package com.example.zg.ui_demo.recyclerview;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.zg.ui_demo.R;

import java.util.List;


public class RecyclerViewAdapter extends RecyclerView.Adapter<ItemViewHolder> {
    private List<DataItem> dateItemList;
    public Context context;//上下文
    OnItemClickListener mOnItemClickListener;
    public interface OnItemClickListener{
        void onItemClick(View view, int position);
    }
    public void setOnItemClickListener(OnItemClickListener listener){
        mOnItemClickListener=listener;
    }


    public void setData(List dateItemList){
        this.dateItemList=dateItemList;
        notifyDataSetChanged();//通知数据集发生变化
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        LayoutInflater layoutInflater = LayoutInflater.from(viewGroup.getContext());

        View itemView = layoutInflater.inflate(R.layout.item_layout,viewGroup,false);
        ItemViewHolder viewHolder = new ItemViewHolder(itemView);
        return viewHolder;

    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder itemViewHolder, final int i) {
        final DataItem dateItem = dateItemList.get(i);
        itemViewHolder.textView1.setText(dateItem.getText1());
        itemViewHolder.textView2.setText(dateItem.getText2());




    }

    @Override
    public int getItemCount() {
        return dateItemList.size();
    }
}
